package de.windeln.marketing.validation;

import javax.annotation.Resource;

import org.springframework.validation.Validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

import com.google.common.base.Strings;

import de.windeln.marketing.campaign.model.RequestClientObject;
import de.windeln.marketing.campaign.services.interfaces.CampaignService;

@Component
public class AlreadyExistingCampaign implements Validator {

	@Resource(name = "campaignService")
	private CampaignService campaignService;

	@Override
	public boolean supports(Class<?> clazz) {
		return clazz == RequestClientObject.class;
	}

	@Override
	public void validate(Object target, Errors errors) {
		RequestClientObject rco = (RequestClientObject) target;
		if (!Strings.isNullOrEmpty(campaignService.findAlreadyExistingCampaign(rco))) {
			errors.rejectValue("campaignName", "Identical Campaign", "Campaign already exist in Shop!");
		}

	}
}
